<template>
  <div class="login-container">
    <div class="login-background" :style="{ backgroundImage: `url(${backgroundImage})` }"></div>
    <div class="login-card">
      <img :src="logo" alt="Logo" class="logo"/>
      <el-card class="login-form">
        <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名">
              <template #prefix>
                <el-icon>
                  <User/>
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" placeholder="密码" type="password">
              <template #prefix>
                <el-icon>
                  <Lock/>
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">登录</el-button>
            <el-button @click="handleGuestLogin">游客登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import {ref, reactive, computed} from 'vue';
import {useRouter} from 'vue-router';
import {useAuthStore} from '../store/auth';
import FingerprintJS from '@fingerprintjs/fingerprintjs';

import {ElMessage} from 'element-plus';
import {User, Lock} from '@element-plus/icons-vue';
import logo from '../../public/logo.png';
import backgroundImage from '../../public/background.png'; // 替换为你的背景图片路径

const router = useRouter();
const authStore = useAuthStore();

const loginForm = ref({
  username: '',
  password: '',
});

const rules = reactive({
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
  ],
});

const loginFormRef = ref(null);

const onSubmit = async () => {
  await loginFormRef.value.validate((valid) => {
    if (valid) {
      // 模拟登录逻辑
      const {username, password} = loginForm.value;
      if (username === 'admin' && password === '123456') {
        //store.user.username = username;
        authStore.login();
        ElMessage.success('登录成功！');
        // 跳转到首页或其他页面
      } else {
        ElMessage.error('用户名或密码错误');
      }
    }
  });
};


const handleGuestLogin = async () => {
  try {
    const fp = await FingerprintJS.load();
    const result = await fp.get();
    const visitorId = result.visitorId;

    console.log(visitorId)

    authStore.loginAsGuest(visitorId);
    router.push('/home/upload');
  } catch (error) {
    console.error('获取浏览器指纹失败:', error);
    // 处理错误，例如显示错误提示
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 96vh;
  position: relative;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: cover; /* 设置背景图片覆盖整个区域 */
  z-index: -1;
}

.login-card {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.logo {
  width: 100px;
  height: 100px;
  margin-bottom: 20px;
  border-radius: 50%; /* 设置圆角 */
  overflow: hidden; /* 隐藏超出圆角的部分 */
  cursor: pointer; /* 设置鼠标指针为手型 */
}

.login-form {
  width: 300px;
}
</style>